{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-04T11:47:39.159229500Z",
     "start_time": "2024-09-04T11:47:39.133715Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['一点水', '两点水', '三点水', '四点水', '五点水']\n",
      "三点水\n",
      "['一点水', '两点水']\n"
     ]
    }
   ],
   "source": [
    "name = ['一点水', '两点水', '三点水', '四点水', '五点水']\n",
    "\n",
    "# 通过索引来访问列表\n",
    "print(name)\n",
    "print(name[2])\n",
    "# 通过方括号的形式来截取列表中的数据\n",
    "print(name[0:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['一点水', '两点水', '三点水', '五点水']\n"
     ]
    }
   ],
   "source": [
    "# 删除列表中的元素\n",
    "del name[3]\n",
    "print(name)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-04T11:47:23.707102700Z",
     "start_time": "2024-09-04T11:47:23.693103100Z"
    }
   },
   "id": "608391e40213467b"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.产品用户\n",
      "['liangdianshui', 'twowater', '两点水']\n",
      "\n",
      "2.统计有多少个用户\n",
      "3\n",
      "\n",
      "3.查看具体的用户\n",
      "liangdianshui,twowater,两点水\n",
      "\n",
      "4.在末尾添加新用户\n",
      "['liangdianshui', 'twowater', '两点水', '茵茵']\n",
      "\n",
      "5.指定位置添加用户\n",
      "['VIP用户', 'liangdianshui', 'twowater', '两点水', '茵茵']\n",
      "\n",
      "6.删除末尾用户\n",
      "['VIP用户', 'liangdianshui', 'twowater', '两点水']\n",
      "\n",
      "7.删除指定位置的list元素\n",
      "['VIP用户', 'twowater', '两点水']\n",
      "\n",
      "8.把某个元素替换成别的元素\n",
      "['VIP用户', 'twowater', '三点水']\n",
      "\n",
      "9.不同元素类型的list数据\n",
      "[['VIP用户', 11111], ['twowater', 22222], ['三点水', 33333]]\n"
     ]
    }
   ],
   "source": [
    "#-*-coding:utf-8-*-\n",
    "#-----------------------list的使用----------------------------------\n",
    "\n",
    "# 1.一个产品，需要列出产品的用户，这时候就可以使用一个 list 来表示\n",
    "user=['liangdianshui','twowater','两点水']\n",
    "print('1.产品用户')\n",
    "print(user)\n",
    "\n",
    "# 2.如果需要统计有多少个用户，这时候 len() 函数可以获的 list 里元素的个数\n",
    "len(user)\n",
    "print('\\n2.统计有多少个用户')\n",
    "print(len(user))\n",
    "\n",
    "# 3.此时，如果需要知道具体的用户呢？可以用过索引来访问 list 中每一个位置的元素，索引是0从开始的\n",
    "print('\\n3.查看具体的用户')\n",
    "print(user[0]+','+user[1]+','+user[2])\n",
    "\n",
    "# 4.突然来了一个新的用户，这时我们需要在原有的 list 末尾加一个用户\n",
    "user.append('茵茵')\n",
    "print('\\n4.在末尾添加新用户')\n",
    "print(user)\n",
    "\n",
    "# 5.又新增了一个用户，可是这个用户是 VIP 级别的学生，需要放在第一位，可以通过 insert 方法插入到指定的位置\n",
    "# 注意：插入数据的时候注意是否越界，索引不能超过 len(user)-1\n",
    "user.insert(0,'VIP用户')\n",
    "print('\\n5.指定位置添加用户')\n",
    "print(user)\n",
    "\n",
    "# 6.突然发现之前弄错了，“茵茵”就是'VIP用户'，因此，需要删除“茵茵”；pop() 删除 list 末尾的元素\n",
    "user.pop()\n",
    "print('\\n6.删除末尾用户')\n",
    "print(user)\n",
    "\n",
    "# 7.过了一段时间，用户“liangdianshui”不玩这个产品，删除了账号\n",
    "# 因此需要要删除指定位置的元素，用pop(i)方法，其中i是索引位置\n",
    "user.pop(1)\n",
    "print('\\n7.删除指定位置的list元素')\n",
    "print(user)\n",
    "\n",
    "# 8.用户“两点水”想修改自己的昵称了\n",
    "user[2]='三点水'\n",
    "print('\\n8.把某个元素替换成别的元素')\n",
    "print(user)\n",
    "\n",
    "# 9.单单保存用户昵称好像不够好，最好把账号也放进去\n",
    "# 这里账号是整数类型，跟昵称的字符串类型不同，不过 list 里面的元素的数据类型是可以不同的\n",
    "# 而且 list 元素也可以是另一个 list\n",
    "newUser=[['VIP用户',11111],['twowater',22222],['三点水',33333]]\n",
    "print('\\n9.不同元素类型的list数据')\n",
    "print(newUser)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-04T11:47:42.241608600Z",
     "start_time": "2024-09-04T11:47:42.212320400Z"
    }
   },
   "id": "636b8261a2fdd468"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
